package com.example.wiki.controller;



import com.example.wiki.req.DocQueryReq;
import com.example.wiki.req.DocSaveReq;
import com.example.wiki.resp.DocQueryResp;
import com.example.wiki.resp.CommonResp;
import com.example.wiki.resp.PageResp;
import com.example.wiki.service.DocService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Api(tags = "文档管理")

@RestController
@RequestMapping("/doc")
public class DocController {


    @Resource
    private DocService docService;

    @ApiOperation("获取所有的文档及其文档排序 ")
    @GetMapping("/all/{ebookId}")
    public CommonResp all(@PathVariable Long ebookId  ){
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>();

        List<DocQueryResp> list = docService.all(ebookId);
        resp.setContent(list);
        resp.setMessage("请求文档分类成功");
        return  resp;
    }
    @ApiOperation("根据bookid获取某篇文档内容")
    @GetMapping("/find-content/{id}")
    public CommonResp findContent(@PathVariable Long id){
        CommonResp<String> resp = new CommonResp<>();

        String content  = docService.findContent(id);
        resp.setContent(content);
        return  resp;
    }

    @GetMapping("/vote/{id}")
    public CommonResp vote(@PathVariable  Long id) {

        CommonResp resp = new CommonResp<>();
        docService.vote(id);
        return resp;
    }

    @ApiOperation("保存文档")
    @PostMapping("/save")
    public  CommonResp save(@Valid @RequestBody DocSaveReq req){
        CommonResp resp = new CommonResp<>();
        docService.save(req);
        return  resp;
    }
    @ApiOperation("删除文档")
    @DeleteMapping("/delete/{idsStr}")
    public  CommonResp delete(@PathVariable String idsStr){
        CommonResp resp = new CommonResp<>();
        String[] stringArray = idsStr.split(",");
        List<Long> listNum = new ArrayList<Long>();
        for (int i = 0; i < stringArray.length; i++) {
            Long num = Long.parseLong(stringArray[i]);
            listNum.add( num);

        }
        docService.delete(listNum);
        return  resp;
    }





}
